new Vue({
    el:'#app',
    data:{
        result:'',
        isDot:false,
        isOperatorAdd:false,
        isStart:true,
        isFirstDot:true,
        isBracket:true,
        LRBracket:true
    },
    methods: {
        //判断是否有算符
        isOperator(char) {
            return ['+','-','×','÷'].indexOf(char) > -1
        },
        //添加数字/运算符号
        append(char){

            //.
            if (char === "." && !this.isDot){
                if (this.isFirstDot && this.isStart){
                    this.result += "0.";
                }else{
                    this.result += '.';
                }
                this.isDot = true;
                this.isStart = false
                this.isOperatorAdd = false;
            }
            //数字
            if (isFinite(char)){
                if (this.isStart){
                    if (char === 0){
                        return
                    }
                    this.result +='' + char;
                    this.isStart = false;
                    this.isOperatorAdd = false;
                    
                }else{
                this.result ='' + this.result + char;
                }
            }
            //运算符
            if (this.isOperator(char) && !this.isOperatorAdd && !this.isStart){
                this.result += '' + char;
                this.isOperatorAdd = true;
                this.isDot = false;
                this.isStart = true;
                this.isFirstDot = true;
            }
        },
        bracket(char){
            if(this.LRBracket){
                this.result += '(';
                this.LRBracket = false;
            }
            else{
                this.result += ')';
                this.LRBracket = true;
            }
        }
        ,
        //计算结果
        calculate(){
            let calculate = this.result.replace(new RegExp('×','g'),'*').replace(new RegExp('÷','g'),'/')
            try{
            this.result = parseFloat(eval(calculate).toFixed(9))
            }catch(err){
                alert("运算公式有误，请重新输入");
                this.result = '';
            }
            try{if (!this.result.indexOf('.')){
                this.isDot = false;
            }}catch{
            }
            this.isOperatorAdd = false;
        },

        //计算+/-
        reverse(){
            this.result = this.result + '* -1';
            this.calculate();
        },
        //清除
        clear(){
        this.result = '';
        this.isDot = false;
        this.isOperatorAdd = false;
        this.isStart = true;
        this.isFirstDot = true;
        this.isBracket = true;
        this.LRBracket = true;
        }
    }
})